package com.yiqi.lottery.feature.find.view.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suozhang.framework.framework.AM;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DateTool;
import com.yiqi.lottery.entity.bo.LotteryHistoryBo;
import com.yiqi.lottery.entity.bo.LotteryResultBo;
import com.yiqi.lottery.entity.enums.LotteryType;

import java.util.Arrays;
import java.util.List;

public class LotteryHistoryAdapter extends BaseQuickAdapter<LotteryHistoryBo, BaseViewHolder> {
  private int position;
    public LotteryHistoryAdapter(int position) {
        super(R.layout.item_find_lottery_result);
        this.position=position;
    }

    @Override
    protected void convert(BaseViewHolder helper, LotteryHistoryBo item) {
        helper.setGone(R.id.tv_find_lottery_result_name, false)
                .setText(R.id.tv_find_lottery_result_qi, "第" + item.getIssue() + "期")
                .setText(R.id.tv_find_lottery_result_date,item.getOpenDate()+"\t("+ DateTool.getWeek(item.getOpenDate())+")");

        RecyclerView rvQiu=helper.getView(R.id.rv_lottery_result_qiu);
        rvQiu.setLayoutManager(new GridLayoutManager(AM.app(),8));
        rvQiu.setNestedScrollingEnabled(false);
        List<String> qiuData= Arrays.asList(item.getCode().replaceAll(",","-").split("-"));
        QiuAdapter qiuAdapter=new QiuAdapter(R.layout.item_find_lottery_result_qiu,qiuData,position);
        rvQiu.setAdapter(qiuAdapter);



        RecyclerView rvFang=helper.getView(R.id.rv_lottery_result_fang);
        rvFang.setLayoutManager(new GridLayoutManager(AM.app(),14));
        rvFang.setNestedScrollingEnabled(false);
        List<String> fangData= Arrays.asList(item.getCode().split(","));
        QiuAdapter fangAdapter=new QiuAdapter(R.layout.item_find_lottery_result_fang,fangData,position);
        rvFang.setAdapter(fangAdapter);

        if (position==0){
            rvQiu.setVisibility(View.VISIBLE);
            rvFang.setVisibility(View.GONE);
        }else{
            rvQiu.setVisibility(View.GONE);
            rvFang.setVisibility(View.VISIBLE);
        }
    }

    class QiuAdapter extends BaseQuickAdapter<String,BaseViewHolder>{
        int position=0;

        public QiuAdapter(int layoutResId, @Nullable List<String> data,int position) {
            super(layoutResId, data);
            this.position=position;
        }


        @Override
        protected void convert(BaseViewHolder helper, String item) {
             helper.setText(R.id.tv_result_qiu,position==0?item:item.substring(item.length()-1,item.length()));
            TextView textView=helper.getView(R.id.tv_result_qiu);
             if (position== LotteryType.大乐透.getIndex()){
                 if (helper.getAdapterPosition()>4){
                     textView.setBackgroundResource(R.drawable.documentary_list_first_blue);
                 }
             }
        }
    }
}
